<template>
  <page-header-wrapper>
    <page-tab-list></page-tab-list>
    <a-card :border='false'>
      <div v-loading='loading'>
        <a-alert message='此配置用于平台微信收款' type='info' show-icon style='margin: 10px 0 20px 15px' />
        <a-form-model
          ref='wechatForm'
          :rules='wechatRules'
          :model='wechatInfo'
          :labelCol='{ span: 3 }'
          :wrapperCol='{ span: 12 }'
        >
          <a-form-model-item label='联盟移动应用APPID' prop='platform_appid'>
            <a-input v-model='wechatInfo.platform_app_appid' type='text' />
          </a-form-model-item>
          <a-form-model-item label='联盟小程序APPID' prop='platform_appid'>
            <a-input v-model='wechatInfo.platform_appid' type='text' />
          </a-form-model-item>
          <a-form-model-item label='联盟小程序Secret' prop='platform_key'>
            <a-input v-model='wechatInfo.platform_key' type='text' />
          </a-form-model-item>
          <a-form-model-item label='商户号' prop='platform_mch_id'>
            <a-input v-model='wechatInfo.platform_mch_id' type='text' />
          </a-form-model-item>
          <a-form-model-item label='kpay_ey' prop='platform_pay_key'>
            <a-input v-model='wechatInfo.platform_pay_key' type='text' />
          </a-form-model-item>
          <a-form-model-item label='apiclient_cert' prop='platform_apiclient_cert'>
            <a-input v-model='wechatInfo.platform_apiclient_cert' :autoSize='{ minRows: 4, maxRows: 6 }'
                     type='textarea' />
          </a-form-model-item>
          <a-form-model-item label='apiclient_key' prop='platform_apiclient_key'>
            <a-input v-model='wechatInfo.platform_apiclient_key' :autoSize='{ minRows: 4, maxRows: 6 }'
                     type='textarea' />
          </a-form-model-item>
          <a-form-model-item :wrapper-col='{ span: 24 }'>
            <a-divider style='margin: 0 0 40px 0' />
            <div style='display: flex; justify-content: center'>
              <a-button :loading='btnLoading' type='primary' @click='submitForm' style='margin-right: 8px'> 保存
              </a-button>
              <a-button @click='$router.back()'> 取消</a-button>
            </div>
          </a-form-model-item>
        </a-form-model>
      </div>
    </a-card>
  </page-header-wrapper>

</template>

<script>
import { getPlatformWechatSetting, setPlatformWechatSetting } from '@/api/saas/setting'
import { templateList } from '@/api/setting/applet'

export default {
  name: 'SaasSettingWechatPay',
  data() {
    return {
      loading: false,
      btnLoading: false,
      wechatInfo: {
        platform_appid: '',
        platform_mch_id: '',
        platform_key: '',
        platform_apiclient_cert: '',
        platform_apiclient_key: '',
        platform_pay_key: '',
        platform_app_appid: ''
      },
      wechatRules: {
        platform_appid: [{ required: true, message: '请输入联盟小程序APPID', trigger: 'blur' }],
        platform_mch_id: [{ required: true, message: '请输入服务商商户号', trigger: 'blur' }],
        platform_key: [{ required: true, message: '请输入联盟小程序Secret', trigger: 'blur' }],
        platform_apiclient_cert: [{ required: true, message: '请输入服务商apiclient_cert证书', trigger: 'blur' }],
        platform_apiclient_key: [{ required: true, message: '请输入服务商apiclient_key证书', trigger: 'blur' }],
        platform_app_appid: [{ required: true, message: '请输入联盟移动应用APPID', trigger: 'blur' }]
      },
      // 微信模板列表
      templateList: []
    }
  },
  created() {
    this.getData()
    // this.getTemplateList()
  },
  methods: {
    getData: function() {
      this.loading = true
      getPlatformWechatSetting({})
        .then((res) => {
          if (res.code == 0) {
            this.wechatInfo = res.data
          }
        })
        .catch((error) => {
          console.log(error)
        })
        .then(() => {
          this.loading = false
        })
    },
    submitForm: function() {
      this.$refs.wechatForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          setPlatformWechatSetting(this.wechatInfo)
            .then((res) => {
              if (res.code == 0) {
                this.$message.success('保存成功！')
              } else {
                this.$message.warning(res.msg ? res.msg : '保存失败！')
              }
            })
            .catch((error) => {
              console.log(error)
            })
            .then(() => {
              this.btnLoading = false
            })
        }
      })
    }
    // 获取微信模板列表
    // async getTemplateList() {
    //   const res = await templateList()
    //   if (res.code == 0) {
    //     this.templateList = res.data.template_list
    //   }
    // },
  }
}
</script>

<style scoped>
</style>
